package com.android.lovingpet.adopt.host.hostinfo.view;

import android.os.Bundle;

import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.lovingpet.R;
import com.android.lovingpet.adopt.host.hostinfo.HostInfoPresenter;
import com.android.lovingpet.adopt.host.hostinfo.IHostInfoContract;
import com.android.lovingpet.base.BaseFragment;
import com.android.lovingpet.bean.Host;
import com.android.lovingpet.util.Utility;
import com.bumptech.glide.Glide;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

import static com.android.lovingpet.adopt.host.hostinfo.HostInfoPresenter.FLAG_Unback_PET_ID;
import static com.android.lovingpet.adopt.host.hostinfo.HostInfoPresenter.FLAG_Undelivered_PET_ID;
import static com.android.lovingpet.adopt.host.hostinfo.HostInfoPresenter.FLAG_Unused_PET_ID;

/**
 * Created by ikata on 2018/4/27.
 */

public class HostInfoFragment extends BaseFragment<IHostInfoContract.View,HostInfoPresenter> implements IHostInfoContract.View, View.OnClickListener {
    @BindView(R.id.bt_consult)
    Button btConsult;
    @BindView(R.id.bt_join)
    Button btJoin;
    Unbinder unbinder;
    @BindView(R.id.iv_video)
    ImageView ivVideo;
    @BindView(R.id.tv_description)
    TextView tvDescription;
    @BindView(R.id.tv_address)
    TextView tvAddress;
    @BindView(R.id.tv_experience)
    TextView tvExperience;
    @BindView(R.id.tv_evaluate)
    TextView tvEvaluate;
    @BindView(R.id.tv_service)
    TextView tvService;
    @BindView(R.id.tv_attention)
    TextView tvAttention;

    private Host host;


    @Override
    protected int setContentView() {
        return R.layout.fragment_host_info;
    }

    @Override
    protected void onCreateView(View view) {
        unbinder = ButterKnife.bind(this, view);
        EventBus.getDefault().register(this);
        setTitle(view,"寄养家庭详情");
        initView();
    }

    public void initView() {
        Glide.with(this).load(host.getImage()).into(ivVideo);
        tvDescription.setText(host.getDescription());
        tvService.setText(host.getService());
        tvAddress.setText(host.getAddress());
        tvExperience.setText(parseExp(host.getPetExp()));
        //家庭评价接口未提供
        tvEvaluate.setText("");
        tvAttention.setText(host.getAttention());
    }

    private String parseExp(String petExp)
    {
        StringBuffer stringBuffer=new StringBuffer();
        try {
            JSONArray jsonArray=new JSONArray(petExp);
            for (int i=0;i<jsonArray.length();i++)
            {
                JSONObject jsonObject=jsonArray.getJSONObject(i);
                String count=jsonObject.getString("count");
                String type=jsonObject.getString("type");
                stringBuffer.append(type).append(count).append("只");
                if (i==jsonArray.length()-1){
                    stringBuffer.append("。");
                }else{
                    stringBuffer.append(",");
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return stringBuffer.toString();
    }


    @Override
    public HostInfoPresenter createPresenter() {
        return new HostInfoPresenter();
    }

    @Override
    public IHostInfoContract.View createView() {
        return this;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
        EventBus.getDefault().unregister(this);
    }

    @OnClick({R.id.bt_consult, R.id.bt_join})
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_consult:
                Toast.makeText(getContext(),"功能开发ing",Toast.LENGTH_LONG).show();
                break;
            case R.id.bt_join:
                Log.d("myId",Utility.getRealId()+":id");
                getPresenter().getUndeliveredPetId(Utility.getRealId(), 1,10);
                break;
            default:
                break;
        }
    }

    @Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
    public void getHost(Host host) {
        this.host = host;
    }

    @Override
    public void next(@Nullable Bundle bundle) {
        assert bundle != null;
        int flag=bundle.getInt("flag");
        switch (flag)
        {
            case FLAG_Undelivered_PET_ID:
                ArrayList<Integer> list=bundle.getIntegerArrayList("ids");
                assert list != null;
                //寄养宠物未寄养出去，跳转指定页面继续完成
                if (list.size()!=0)
                {
                    getFragmentManager().beginTransaction().addToBackStack(null).replace(R.id.host_info_content,new FosterFinishFragment()).commit();
                }
                else
                {
                   getPresenter().getUnbackedPetId(Utility.getRealId(),1,10);
                }
            break;
            case FLAG_Unused_PET_ID:
                ArrayList<Integer> list2=bundle.getIntegerArrayList("ids");
                if (list2 == null) {
                    return;
                }
                if (list2.size()!=0)
                {
                    getFragmentManager().beginTransaction().addToBackStack(null).replace(R.id.host_info_content, new ContactWayFragment()).commit();
                }
                else
                {
                    Toast.makeText(getContext(),"请您在我的板块的宠物里面添加自己的宠物！",Toast.LENGTH_LONG).show();
                }

                break;
            case FLAG_Unback_PET_ID:
                ArrayList<Integer> list1=bundle.getIntegerArrayList("ids");
                if (list1 == null) {
                    return;
                }
                //寄养宠物未回家，跳转指定页面继续完成
                if (list1.size()!=0)
                {
                    getFragmentManager().beginTransaction().addToBackStack(null).replace(R.id.host_info_content,new BackHomeFragment()).commit();
                    EventBus.getDefault().postSticky(list1.get(0));
                }
                else
                {
                   getPresenter().getUnusedPetId(Utility.getRealId(),1,10);
                }
                break;
        }
    }
}
